package com.example.myerp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myerp.dto.S_pay_Dto;
import com.example.myerp.mapper.S_pay_detatilsMapper;
import com.example.myerp.pojo.Inbound_Scheduling;
import com.example.myerp.pojo.S_pay;
import com.example.myerp.mapper.S_payMapper;
import com.example.myerp.pojo.S_pay_detatils;
import com.example.myerp.service.S_payService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhuxingyu
 * @since 2021-08-10
 */
@Service
@SuppressWarnings("all")
public class S_payServiceImpl extends ServiceImpl<S_payMapper, S_pay> implements S_payService {


    @Autowired
    S_payMapper sPayMapper;

    @Autowired
    S_pay_detatilsMapper sPayDetMapper;


    @Override
    public void addS_pay(S_pay_Dto s_pay_dto) {
        S_pay s_pay = s_pay_dto.getS_pay();
        s_pay.setPayId(UUID.randomUUID().toString());
        sPayMapper.addSpay(s_pay);
        Long id = s_pay.getId();
        List<S_pay_detatils> detatils = s_pay_dto.getS_pay_detatils();
        detatils.forEach(data -> {
            data.setParentId(id);
            sPayDetMapper.addSpayDet(data);
        });
    }

    @Override
    public IPage<S_pay> inbRegList(Long pageNum, Long pageSize) {
        Page<S_pay> page = new Page<>(pageNum, pageSize, true);
        return sPayMapper.inbRegList(page);
    }

    @Override
    public IPage<S_pay> regchekspay(Long pageNum, Long pageSize) {
        Page<S_pay> page = new Page<>(pageNum, pageSize, true);
        return sPayMapper.regchekspay(page);
    }

    @Override
    public IPage<S_pay> selSpyInb(Long pageNum, Long pageSize) {
        Page<S_pay> page = new Page<>(pageNum, pageSize, true);
        return sPayMapper.selSpyInb(page);
    }

    @Override
    public IPage<S_pay> checkCell(Long pageNum, Long pageSize) {
        Page<S_pay> page = new Page<>(pageNum, pageSize, true);
        return sPayMapper.checkCell(page);
    }

    @Override
    public void checkSpay(S_pay s_pay) {
        sPayMapper.checkSpay(s_pay);
    }

    @Override
    public void checkSpayNo(S_pay s_pay) {
        sPayMapper.checkSpayNo(s_pay);
    }

    @Override
    public Integer updInbStaSpay(Inbound_Scheduling ins) {
        Long id = ins.getId();
        Long parentId = ins.getParentId();
        sPayDetMapper.updSpayDet(ins.getNum(), id);
        Integer selStatus = sPayDetMapper.selStatus(parentId);
        Integer staedSon = sPayDetMapper.selStaedSon(parentId);
        Integer row = 0;
        if (selStatus == staedSon) {
            sPayMapper.updInbStaSpay(parentId);
            row = 1;
        }
        return row;
    }

    @Override
    public void sPayRegCell(S_pay_Dto s_pay_dto) {
        List<Inbound_Scheduling> ins = s_pay_dto.getIns();
        Integer sum = 0;
        Long parentId = null;
        for (int i = 0; i < ins.size(); i++) {
            Integer num = ins.get(i).getNum();
            Long id = ins.get(i).getId();
            sum = sum + num;
            parentId = ins.get(i).getParentId();
            sPayDetMapper.spayDetRegCell(num, id);
        }
        sPayMapper.sPayRegCell(sum, parentId);
    }

    @Override
    public void sPayCellOver(S_pay_Dto s_pay_Dto) {
//        S_pay s_pay = s_pay_Dto.getS_pay(F);
        List<Inbound_Scheduling> det = s_pay_Dto.getIns();
        Integer sum = 0;
        for (int i = 0; i < det.size(); i++) {
            Integer paidAmount = det.get(i).getPaidAmount();
            Long id = det.get(i).getId();
            Integer productId = Integer.valueOf(det.get(i).getProductId());
            sum = sum + paidAmount;
            sPayDetMapper.sPayDetCellOver(paidAmount, id);
            sPayDetMapper.updCell(productId, paidAmount);
        }
        S_pay s_pay = s_pay_Dto.getS_pay();
        s_pay.setPaidAmountSum(sum);
        sPayMapper.sPayCellOver(s_pay);

    }

    @Override
    public IPage<S_pay> spayCellInfo(Long pageNum, Long pageSize) {
        Page<S_pay> page = new Page<>(pageNum, pageSize);
        return sPayMapper.spayCellInfo(page);
    }


}
